package com.sinopec.basemodule.camera;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.sinopec.basemodule.R;

public class CameraPreviewActivity extends AppCompatActivity {
    private SubsamplingScaleImageView mImageSsv;
    private LinearLayout mCameraCaptureSuccessLl;
    private TextView mCancelTv;
    private String mImageUrl;

    public static void startActivityForResult(AppCompatActivity activity, @NonNull String imageUrl, int requestCode) {
        Intent intent = new Intent(activity, CameraPreviewActivity.class);
        intent.putExtra("imageUrl", imageUrl);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (window != null) {
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_camera_prew);

        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }

        if (!intent.hasExtra("imageUrl")) {
            finish();
            return;
        }

        mImageUrl = intent.getStringExtra("imageUrl");

        initView();
        initEvent();
    }

    private void initEvent() {
        //重拍
        mCancelTv.setOnClickListener(v -> finish());

        //提交数据
        mCameraCaptureSuccessLl.setOnClickListener(v -> commitMethod());
    }

    /**
     * 提交数据
     */
    private void commitMethod() {
        Intent intent = new Intent();
        intent.putExtra(CameraActivity.IMAGE_URL, mImageUrl);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void initView() {
        mImageSsv = findViewById(R.id.image_ssv);
        mCameraCaptureSuccessLl = findViewById(R.id.camera_capture_success_ll);
        mCancelTv = findViewById(R.id.cancel_tv);
        mImageSsv.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
        mImageSsv.setImage(ImageSource.uri(mImageUrl));
    }
}
